package chihane.jdaddressselector;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;


public class BottomDialog extends Dialog {
    private AddressSelector selector;

    public BottomDialog(Context context) {
        super(context, R.style.bottom_dialog);
        init(context);
    }

    public BottomDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    public BottomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {
//        final BottomDialog dialog = new BottomDialog(context, R.style.bottom_dialog);
        selector = new AddressSelector(context);

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_select_address,
                null);
        setContentView(view);
        FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.layout_total);
        frameLayout.addView(selector.getView());
        TextView btnCancel = (TextView) view.findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BottomDialog.this.dismiss();
            }
        });
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setGravity(Gravity.BOTTOM);
    }


    public static int dip2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


    public void setOnAddressSelectedListener(OnAddressSelectedListener listener) {
            this.selector.setOnAddressSelectedListener(listener);
    }

//    public static BottomDialog show(Context context) {
//        return show(context, null);
//    }
//
//    public static BottomDialog show(Context context, OnAddressSelectedListener listener) {

//        dialog.show();
//
//        return dialog;
//    }
}
